$(function() {
	/*
	 * 查看流程图，附件下载弹窗
	 */
	$(".matters-detail-attach").click(function(e) {
		var ele = e.target || e.srcElement; //兼容ie9或以下版本自定义属性
		var mattersDetailAttachJson = "";
		if(ele.getAttribute("data-attach")) {
			mattersDetailAttachJson = ele.getAttribute("data-attach");
		} else {
			mattersDetailAttachJson = ele.parentNode.getAttribute("data-attach"); //点到了下载图标
		}
		var mattersDetailAttach = JSON.parse(mattersDetailAttachJson);
		
		if(mattersDetailAttach.length <= 1) {
			dowmloadJump(mattersDetailAttach[0].FILEPATH);
		} else {
			$("#modal-list").empty();
			var html = [];
			$.each(mattersDetailAttach, function(i, value){
				html.push("<tr>");
				html.push("<td>"+(i+1)+"</td>");
				html.push("<td>"+value.ATTACHNAME+"</td>");
				html.push('<td><a href="javascript:void(0);" onclick="dowmloadJump(\''+value.FILEPATH+'\')">下载<i class="icon icon-download"></i></a></td>');
				html.push("</tr>");
			});
			$("#modal-list").append(html.join(""));
		}
	});
});

/**
 * 附件下载选择跳转方式
 * @param url
 */
function dowmloadJump(url) {
	var mb = myBrowser();
	if('IE' == mb){
		window.location.href = url;
	}else{
		window.open(url,"_blank");
	}
}

/**
 * 	办事指南收藏
 * @param itemCode
 * @param user_id
 */
function guideCollection(itemCode,sxmc,zgbm,xzqhmc,cnsx,cnsxdw){
		$.ajax({
			type : "POST",
			url: ctx+"/guide-collect",
			data: {itemCode:itemCode,sxmc:sxmc,zgbm:zgbm,xzqhmc:xzqhmc,cnsx:cnsx,cnsxdw:cnsxdw},
			dataType : 'JSON',
			success:function(data){
				favouritedStyle();
				if(data.status == "success"){
					alert({msg:"收藏成功，可到个人中心查看已收藏的事项。", btnOkClick:function(){
						window.location.reload();
					}});
				}else{
					alert({msg:"请重新登录！", btnOkClick:function(){
						login();
					}});
				}
			}
		});
}
/**
 * 	取消办事指南收藏
 * @param itemCode
 * @param user_id
 */
function cancelCollection(itemCode,user_id){
	$.ajax({
		type : "POST",
		url: ctx+"/cancel-collect",
		data: {itemCode:itemCode,user_id:user_id},
		dataType : 'JSON',
		success:function(data){
			if(data.status == "success"){
				favouritedStyle();
				alert({msg:"取消收藏成功！", btnOkClick:function(){
					window.location.reload();
				}});
			}else{
				alert({msg:"请重新登录！", btnOkClick:function(){
					login();
				}});
			}
		}
	});
}
/**
 * 办事指南收藏样式
 * @returns
 */
function favouritedStyle(){
	var $this = $(".matters-favourite");
    var isFavourited = $this.hasClass('favourited');
    if (isFavourited) {
      $this.removeClass('favourited').html('收藏指南');
    } else {
      $this.addClass('favourited').html('已收藏');
    }
}
 
//根据根据不同的星星个数加载不同的字典
function loadCheckbox(iStar){
	$("#grade").val(iStar);
	var superCode = "";
	//根据不同的进行后台获取
	if(iStar=='1'){
		superCode ="001";
	}else if(iStar=='2'){
		superCode ="002";
	}else if(iStar=='3'){
		superCode ="003";
	}else if(iStar=='4'){
		superCode ="004";
	}else if(iStar=='5'){
		superCode ="005";
	}
	
	$.ajax({
		url : ctx + "/evaluate/getArtificiaEevaluate.do" ,
		type : 'post' ,
		data : {kind:'ZTP_PJ_BSZN',superCode:superCode} ,
		async: false,
		success : function(data){
			var html = "";
		    $.each(data,function(i,entity){
		    	if(iStar=='1'||iStar=='2'){
				 	html +="<p><label class='form-checkbox-wrapper'><input type='checkbox' name='PY_ITEM_DETAIL_CODE' value='"+entity.CODE+"' id='"+entity.DETAIL+"'/><span>"+entity.DETAIL+"</span></label></p>";
		    	}else{//三星以默认全选标签，不可编辑
		    		html +="<p><label class='form-checkbox-wrapper'><input type='checkbox' checked='checked' disabled='disabled' name='PY_ITEM_DETAIL_CODE' value='"+entity.CODE+"' id='"+entity.DETAIL+"'/><span>"+entity.DETAIL+"</span></label></p>";
		    	}
		    })
		    $(".grade-check").empty();
		    $(".grade-check").append(html);
		    $(".grade-check").show();
		    $("#textarea").show();
		}
	});
}

function checkIsLogin(){
	var b = false;
	 $.ajax({
		url : ctx + "/userCenter/getCurrentUser.do",
		type : 'post' ,
		async : false,
		data : { } ,
		success : function(data){
			if(data != 'null' && data != '' && typeof(data) != undefined){
				//登录
				b = true;
			}
		},
		error:function(){
			alert("信息获取失败");
		}
	});
	return b;
}

//保存评议
function submit(){
	var isLogin = checkIsLogin();
	if(isLogin){
		//评议完成后隐藏按钮
		confirm({msg:"您确定提交评议吗？", btnOkClick:function(){
			saveEvaluate();
		}});
	}else{
		alert({msg:"您还没有登录，请先登录！", btnOkClick:function(){
			location.href = ctx + '/oauth/sso/ancle.do?url=' + location.href ;
			return;
		}});
	}
}

function saveEvaluate(){
	var stime = $("input[name=stime]").val();
	var nonce = $("input[name=nonce]").val();
	var sign = $("input[name=sign]").val();
	//评议实体对象（事项信息）
	var pyInfo = new Object();
	var kind = "ZTP_PJ_BSZN";
	pyInfo.py_dic_kind = kind;
	pyInfo.sxmc = $("#itemName").val();
	pyInfo.deptNo = $("#deptNo").val();
	pyInfo.itemCode = $("#itemCode").val();
	pyInfo.itemVersion = $("#itemVersion").val();
	pyInfo.deptName = $("#deptName").html();
	pyInfo.blbm = $("#deptName").html();
	pyInfo.py_object = $("#itemName").val();
	pyInfo.py_type = "3";//办事指南评议代码：3
	pyInfo.py_content = $.trim($("#textarea").val());
	pyInfo.py_gdbh = "0303";//办事指南评议工单编号开头，在后台还有拼接
	var pyItem = new Object();
	pyItem.py_dic_kind = kind;
	pyItem.py_item_code = "111";//
	pyItem.py_item_name = "办事指南评议";
	pyItem.py_star_level = $("#grade").val();
	var pyItemDetailList = [];
	$(".grade-check").find("input[type=checkbox]").each(function(iStar, oStar) {
		if($(oStar).is(":checked")) {
			var pyItemDetail = new Object();
			pyItemDetail.py_dic_kind = kind;
			pyItemDetail.py_item_detail_code = $(oStar).val();
			pyItemDetail.py_item_detail_name = $(oStar).attr("id");
			pyItemDetailList.push(pyItemDetail);
		}
	});
	pyItem.pyItemDetailList = pyItemDetailList;
	var pyItemList = [];
	pyItemList.push(pyItem);
	pyInfo.pyItemList = pyItemList;
	$.ajax({
		url : ctx + "/evaluate/saveEvaluate.do?stime="+stime+"&nonce="+nonce+"&sign="+sign,
		type : 'post' ,
		data : {"jsonstr":JSON.stringify(pyInfo)} ,
		success : function(data) {
			if(!data.error){
				alert({msg:"评议成功！", btnOkClick:function(){
					window.location.reload();
				}});
			} else {
				$(".error").empty();
				$(".error").append('<i class="icon icon-error"></i>'+data.errorMessage);
				$(".error").show();
			}
		},
		error:function(){
			var msg = $("#securityErrorMsg").val();
			if(msg != null && msg != "") {
				$(".error").empty();
				$(".error").append('<i class="icon icon-error"></i>'+msg);
				$(".error").show();
			}
		}
	});
}

/**
 * 网上办理（在线申办）跳转
 * @param url	申办地址(在线办理地址)
 * @param matterType	事项类型(对应事项目录的"8.1.7 服务对象":1.自然人,2.企业法人,3.行政机关,4.事业法人及其他社团组织；可多选)
 */
function onlineServiceApply(url, matterType){
	var newWindow = window.open();
	newWindow.opener = null;
	newWindow.location = url;
}

/**
 * 将办事指南页面以pdf文件形式下载到本地
 * 
 * @param taskCode
 *            事项编码
 */
function downloadPdf(taskCode) {
	if (!taskCode) {
		alert("事项编码为空");
		return false;
	}

	// ajax不支持文件下载，故采用表单提交方式
	var ation = ctx + "/guide/download-pdf/" + taskCode;
	var formEle = '<form action="' + ation + '" method="get">' + // action请求路径及推送方法
				  '</form>';
	jQuery(formEle).appendTo('body').submit().remove();
}
